package com.tanhua.server.controller;

import cn.hutool.core.util.RandomUtil;
import com.tanhua.autoconfig.templates.SmsTemplate;
import com.tanhua.server.server.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/user")
public class LoginController {
    @Autowired
    private SmsTemplate smsTemplate;
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Autowired
    private UserService userService;
    @PostMapping("/login")
    public void login(@RequestBody Map map){
   String phone = (String)map.get("phone");
   String code = "123456";
   //todo 生产环境在放开
//   String code = RandomUtil.randomNumbers(6);
//   smsTemplate.sendMsg(phone,code);
        System.out.println("验证码"+code);
        redisTemplate.opsForValue().set("phone:"+phone,code,5,TimeUnit.MINUTES);
    }
    @PostMapping("/loginVerification")
    public Map loginVerificaion(@RequestBody Map map) {
        String phone = map.get("phone").toString();
        String code = map.get("verificationCode").toString();
        Map req = userService.loginVerificaion(phone, code);
        return req;
    }
}
